{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "from iqsopenapi import *\n",
    "import datetime"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "def GetHisBar(self, symbol, exchange, barType, startTime, endTime):\n",
    "    \"\"\"获取历史K线数据\"\"\"\n",
    "    if not symbol:\n",
    "        logger.error(\"合约代码不能为空\")\n",
    "        return None\n",
    "    url = Environment.get_instance().get_apiurl('api/MarketData/GetHisBar')\n",
    "    params = {'symbol':symbol,'exchange':exchange,'dataType':barType,'begin':startTime,'end':endTime}\n",
    "    resp = httpJsonPost(url,params) \n",
    "    strParams = json.dumps(params,ensure_ascii=False)\n",
    "    if not resp:\n",
    "        logger.error(\"response：\" + url + \",\" + strParams)\n",
    "        return None\n",
    "    js = json.loads(resp,encoding='utf-8')\n",
    "    if js.get('error_no') != 0:\n",
    "        logger.error(\"request:\" + url + \",\" + strParams + \"response：\" + resp)\n",
    "        return None\n",
    "    data = js.get('data')\n",
    "    if not data:\n",
    "        return None\n",
    "    result = []\n",
    "    for x in data:\n",
    "        bar = self.__ToBar(x)\n",
    "        result.append(bar)\n",
    "    return result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'ctx' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-9-2293105f9088>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mbars2\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mctx\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mget_history_bars\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'rb2010.SHFE'\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;34m'1m'\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0mdatetime\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;36m2020\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m5\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m21\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m9\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m10\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m0\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0mdatetime\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;36m2020\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m5\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m21\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m11\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m10\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m0\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m      2\u001b[0m \u001b[0mbars2\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mNameError\u001b[0m: name 'ctx' is not defined"
     ]
    }
   ],
   "source": [
    "bars2 = ctx.get_history_bars('rb2010.SHFE','1m',datetime(2020,5,21,9,10,0),datetime(2020,5,21,11,10,0))\n",
    "bars2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "ename": "SyntaxError",
     "evalue": "invalid syntax (<ipython-input-10-664ee84f816d>, line 4)",
     "output_type": "error",
     "traceback": [
      "\u001b[1;36m  File \u001b[1;32m\"<ipython-input-10-664ee84f816d>\"\u001b[1;36m, line \u001b[1;32m4\u001b[0m\n\u001b[1;33m    iqsopenapi.\u001b[0m\n\u001b[1;37m               ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m invalid syntax\n"
     ]
    }
   ],
   "source": [
    "# !pip install iqsopenapi\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
